<template>
  <div class="action-panel">
    <div class="header-container">
      <div class="simple-header-left">
        <a-tooltip content="返回">
          <div class="simple-header-icon" @click="handleBack">
            <icon-left size="24" />
          </div>
        </a-tooltip>
        <a-typography-paragraph bold class="simple-header-input" editable v-model:editText="name">
          {{ name }}
        </a-typography-paragraph>
      </div>
      <div class="simple-header-right pr-5 flex gap-2">
        <a-button class="primary-btn" size="mini" @click="handleSave">保存</a-button>
        <a-button class="primary-btn" size="mini" @click="goEdit">新建大屏</a-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, watch } from "vue"
import { useRouter } from "vue-router"

export default {
  name: "cube-header",
  emits: ["update:modelValue", "save", "back"],
  props: {
    modelValue: {
      type: String,
      default: ""
    },
    backUrl: {
      type: String
    },
    pageStatus: {
      type: Object,
      default: () => ({})
    }
  },
  setup(props, { emit }) {
    const name = ref("")
    const router = useRouter()
    const handleBack = () => {
      emit("back", props.backUrl)
    }

    const handleSave = () => {
      emit("save")
    }

    const handleInputChange = (e) => {
      emit("update:modelValue", e.target.value.trim())
    }
    const goEdit = () => {
      const route = router.resolve({
        path: "/visualEditor",
        query: { cube_id: props.pageStatus.id }
      })
      window.open(route.href, "_blank")
    }
    watch(
      () => props.modelValue,
      (val) => {
        name.value = val
      },
      { immediate: true }
    )

    return {
      name,
      handleSave,
      handleInputChange,
      handleBack,
      goEdit
    }
  }
}
</script>

<style scoped lang="less">
.action-panel {
  height: 50px;
  display: flex;
  background-color: #1d1e1f;
  color: #fff;

  .header-container {
    width: 100%;
    height: 50px;
    background-color: #1d1e1f;
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;

    .simple-header-left,
    .simple-header-right {
      height: 100%;
      display: flex;
      align-items: center;
    }

    .simple-header-icon {
      cursor: pointer;
      border-right: 1px solid hsla(0, 0%, 100%, 0.2);
      height: 50px;
      width: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .simple-cube-icon {
      height: 20px;

      > i {
        margin: 0 10px 0 14px;
        font-size: 20px;
      }
    }

    .simple-header-input {
      background-color: transparent;
      font-size: 16px;
      height: 30px;
      min-width: 30px;
      border: 1px solid transparent;
      border-radius: 0;
      color: #fff;
      line-height: 30px;
      padding: 0 11px;
      outline: none;
      margin-bottom: 0 !important;
      :deep(.arco-typography-edit-content) {
        margin: 0 !important;
        padding: 0 !important;
      }
      &:focus,
      &:active,
      &:focus-visible {
        border: 1px solid var(--bin-color-primary);
      }
    }
  }

  .cube-btn {
    height: 100%;

    .primary-btn {
      cursor: pointer;
      display: inline-block;
      width: 160px;
      height: 28px;
      text-align: center;
      line-height: 26px;
      color: #ffffff;
      background-color: var(--bin-color-primary);
      border: 1px solid var(--bin-color-primary);
      border-radius: 14px;

      + .primary-btn {
        margin-left: 14px;
      }
    }
  }

  .cube-navigator {
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 108px;
    height: 100%;
    cursor: pointer;
    font-size: 12px;

    &:hover {
      background: #000;
    }

    > i {
      margin-right: 4px;
    }
  }
}
</style>
